package com.zhangfei.controller;

import com.alibaba.fastjson.JSON;
import com.zhangfei.entity.Coupon;
import com.zhangfei.grpc.server.CouponServerResponse;
import com.zhangfei.grpc.server.CouponServiceGrpc;
import com.zhangfei.grpc.server.DeductCouponRequest;
import com.zhangfei.utils.CouponUtil;
import io.grpc.stub.StreamObserver;

import net.devh.boot.grpc.server.service.GrpcService;

@GrpcService
public class CouponGrpcServer extends CouponServiceGrpc.CouponServiceImplBase{
    @Override
    public void deductProductInventory(DeductCouponRequest request, StreamObserver<CouponServerResponse> responseObserver) {
        int couponId = request.getCouponId();

        //查找优惠券详细信息
        Coupon coupon = CouponUtil.getCouponById(couponId);
        String jsonData = JSON.toJSONString(coupon);
        CouponServerResponse couponServerResponse = CouponServerResponse.newBuilder()
                .setCode(200)
                .setMessage("")
                .setData(jsonData)
                .build();
        responseObserver.onNext(couponServerResponse);
        responseObserver.onCompleted();
    }
}
